package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理请求和响应的方法
 *
 * @version V1.0
 * @package com.leyou.item.web
 * @description:
 * @author: pc
 * @date: 2019/6/25 14:51
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    /**
     * key  搜索的关键词
     * page 当前页
     * rows 显示行数
     * sortBy 排序的列
     * desc 升序
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",required = false) Boolean desc
    ) {
       PageResult<BrandDTO> result =  brandService.queryBrandByPage(key,page,rows,sortBy,desc);
       return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Void> insertBrand(BrandDTO brandDTO , @RequestParam("cids") List<Long> cids) {
        brandService.insertBrand(brandDTO,cids);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long cid) {
        List<BrandDTO> result = brandService.queryBrandByCategoryId(cid);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id){
        BrandDTO brand = brandService.queryById(id);
        return ResponseEntity.ok(brand);
    }


    /**
     * 根据ids查询品牌
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {

        return ResponseEntity.ok(brandService.queryBrandByIds(ids));

    }

}
